package com.xianyun.site.iblog.blog.executor.query;

import com.alibaba.cola.dto.SingleResponse;
import com.xianyun.common.core.utils.StringUtils;
import com.xianyun.common.core.utils.bean.BeanUtils;
import com.xianyun.site.iblog.blog.common.exception.ArticleNotFoundException;
import com.xianyun.site.iblog.gatewayimpl.database.ArticleMapper;
import com.xianyun.site.iblog.gatewayimpl.database.ColumnMapper;
import com.xianyun.site.iblog.gatewayimpl.database.TagsMapper;
import com.xianyun.site.iblog.domain.user.gateway.SystemUserGateway;
import com.xianyun.site.iblog.dto.blog.ArticleViewByUrlQry;
import com.xianyun.site.iblog.dto.blog.data.ColumnDTO;
import com.xianyun.site.iblog.dto.blog.data.TagsDTO;
import com.xianyun.site.iblog.co.blog.ArticleCO;
import com.xianyun.site.iblog.gatewayimpl.database.domainobject.ArticleDO;
import com.xianyun.site.iblog.gatewayimpl.database.domainobject.ColumnDO;
import com.xianyun.site.iblog.gatewayimpl.database.domainobject.TagsDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Component;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import static java.util.stream.Collectors.toList;

/**
 * 获取具体的文章详情
 * @author mac_zyj
 */
@Component
public class ArticleViewByUrlQryExo {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private TagsMapper tagsMapper;

    @Autowired
    private ColumnMapper columnMapper;

    @Autowired
    private SystemUserGateway systemUserGateway;

    /**标签转换复制器*/
    private final  BeanCopier tagsCopier = BeanCopier.create(TagsDO.class,TagsDTO.class,false);

    /**分栏转换复制器*/
    private final  BeanCopier columnCopier = BeanCopier.create(ColumnDO.class,ColumnDTO.class,false);

    public SingleResponse<ArticleCO> execute(@Valid ArticleViewByUrlQry qry){
        ArticleCO articleCo =getArticleShow(qry);
        return SingleResponse.of(articleCo);
    }

    private ArticleDO getArticleInfo(ArticleViewByUrlQry qry){
        Integer userId =systemUserGateway.getUserIdByUserUniquePath(qry.getUserUniquePath());
        if (null != userId){
            ArticleDO article = articleMapper.getArticleByUrlUnique(userId, qry.getUniqueId());
            if ( null != article){
                return article;
            }
        }
        throw new ArticleNotFoundException("文章已去了火星，请查看路径是否正确!");
    }

    private ArticleCO getArticleShow(ArticleViewByUrlQry qry){
        ArticleDO article = getArticleInfo(qry);
        ArticleCO showView = new ArticleCO();
        BeanUtils.copyProperties(article,showView);
        showView.setTags(getTagsList(article.getId()));
        showView.setColumns(getColumnList(article.getId()));
        if (null!= qry.getUid()&& article.getUid().equals(qry.getUid().toString())){
            showView.setIsCreator(1);
        }
        return showView;
    }

    private List<TagsDTO> getTagsList(Integer articleId){
        List<TagsDO> tagsList = tagsMapper.selectTagListByArt(articleId);
        if (!tagsList.isEmpty()){
            return tagsList.stream().map(tags -> {
                TagsDTO obj = new TagsDTO();
                tagsCopier.copy(tags,obj,null);
                return obj;
            }).collect(toList());
        }
        return new ArrayList<>();
    }

    private List<ColumnDTO> getColumnList(Integer articleId){
        List<ColumnDO> columnList = columnMapper.selectColumnListByArt(articleId);
        if (!columnList.isEmpty()){
            return columnList.stream().map(column -> {
                ColumnDTO obj = new ColumnDTO();
                columnCopier.copy(column,obj,null);
                return obj;
            }).collect(toList());
        }
        return new ArrayList<>();
    }
}
